import Utils from "./Util";
import GameManager from "./GameManager";

export default class BaseCom extends cc.Component{
    private clickEventList = [];

    public onDestroy(){
        this.onHide();
    }

    public doCtor(){

    }

    onShow(){

    }

    onHide(){
        if(this.clickEventList){
            for(var i = 0;i < this.clickEventList.length; i ++){
                Utils.removeEventListener(this.clickEventList[i]);
            }
            this.clickEventList.length = 0;
            this.clickEventList = null;
        }

        GameManager.getInstance().eventCenter.RemoveEvent(this.uuid);
    }

    addDynamicComponent(node, component){
    }

    registerEvent(key, callback){
        GameManager.getInstance().eventCenter.RegisterEvent(this.uuid, key, callback);
    }

    dispatchEvent(key, value){
        GameManager.getInstance().eventCenter.DispatchEvent(key, value);
    }

    dispatchEventSync(key, value){
        GameManager.getInstance().eventCenter.DispatchEventSync(key);
    }

    registerClickEvent(target, callback){
        if(target){
            Utils.addEventListener(target, callback);
            this.clickEventList.push(target);
        }
    }

    removeClickEvent(node){
        for(var i = 0;i < this.clickEventList.length; i ++){
            if(this.clickEventList[i] == node){
                this.clickEventList.splice(i, 0);
                Utils.removeEventListener(node);
            }
        }

    }

    frameOnMove(dt){
    }

    // update (dt) {},
};
